package persentation.presentLogin;

import java.awt.AWTEvent;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.nio.channels.spi.SelectorProvider;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import persentation.presentAdministrator.AdministratorFrame;
import persentation.presentAdministrator.MyTabbedPane;
import persentation.presentDepartmentDean.DepartmentDeanFrame;
import persentation.presentStudent.StudentFrame;
import persentation.presentTeacher.TeacherFrame;
import persentation.presentUniversityDean.UniversityDeanFrame;
import po.userPO.UserPO;
import dataservice.ResultMessage;
import dataservice.userDataService.UserDatabaseService;
import businesslogic.userBl.UniversityDean;
import businesslogic.userBl.UserController;



public class LoginFrame extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPanel;
	private JButton loginButton;
	private JComboBox<String> selectBox;
	private JLabel selectLabel,idLabel,passwordLabel,isIdLabel,isPasswordLabel;
	private JTextField idField;
	private JPasswordField passwordField;
	private Title title;
	private ImageIcon image;
	private UserController userController;
//	private ImageIcon bottomIcon;
	private JLabel bottomLabel;

	private MyTabbedPane tabbedPane;
	private String userID;
	
	private int userType;
	
	private JFrame newFrame;
	
	
	/**
	 * Create the frame.
	 */
	public LoginFrame() {
		userController=new UserController(userID);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		
		setTitle("登录南京大学选课系统");
		setSize(643,489);
		System.out.println("aaaaaa");

		
		System.out.println("bbbbb");
		
		Toolkit tookit=Toolkit.getDefaultToolkit();
		Dimension screen=tookit.getScreenSize();
		int x=(int)(screen.getWidth()-this.getWidth())>>1;
		int y=(((int) ((screen.getHeight()-this.getHeight())-15)>>1));
		setLocation(x,y);
		
		contentPanel=new JPanel();
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPanel);
		contentPanel.setLayout(null);

		Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
			
			@Override
			public void eventDispatched(AWTEvent event) {
				 if(((KeyEvent)event).getID()==KeyEvent.KEY_PRESSED){ 
					 if(((KeyEvent)event).getKeyCode()==KeyEvent.VK_ENTER)
						 sendRequest();
					 
				 }
				
			}
		},AWTEvent.KEY_EVENT_MASK);
		
		
		selectLabel=new JLabel("登录身份");
		selectLabel.setVerticalAlignment(SwingConstants.TOP);
		selectLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
		selectLabel.setBounds(new Rectangle(151, 141, 90, 30));	
		contentPanel.add(selectLabel);
	
		idLabel=new JLabel("学工号");
		idLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
		idLabel.setBounds(new Rectangle(161, 198, 60, 30));
		contentPanel.add(idLabel);
		
		passwordLabel=new JLabel("密码");
		passwordLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
		passwordLabel.setBounds(new Rectangle(173, 253, 46, 30));
		contentPanel.add(passwordLabel);
		
		isIdLabel=new JLabel();
		
		isIdLabel.setFont(new Font("微软雅黑",Font.PLAIN,20));
		isIdLabel.setBounds(getBounds());
		
		isPasswordLabel=new JLabel();
		isPasswordLabel.setFont(new Font("微软雅黑",Font.PLAIN,20));
		isPasswordLabel.setBounds(getBounds());
				
		selectBox=new JComboBox<String>();
		userType=-1;
		selectBox.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				if(e.getStateChange()==ItemEvent.SELECTED)
					userType=selectBox.getSelectedIndex();	
				
			}
		});
		selectBox.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				userType=selectBox.getSelectedIndex();
//				System.out.println(userType);		
			}
		});
		selectBox.setBounds(new Rectangle(301, 140, 177, 30));
		selectBox.setModel(new DefaultComboBoxModel<String>(new String[]{"学生","任课教师","院系教务老师","教务处老师","系统管理员"}));
		selectBox.setFont(new Font("微软雅黑",Font.PLAIN,20));
		contentPanel.add(selectBox);
	
		
		
		
		loginButton=new JButton("登录");
		
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				sendRequest();
				
			}
		});
		loginButton.setFont(new Font("微软雅黑", Font.BOLD, 20));
		loginButton.setBounds(173, 344, 90, 30);
		contentPanel.add(loginButton);
		
		idField=new JTextField();
		idField.setFont(new Font("微软雅黑",Font.PLAIN,20));
		idField.setBounds(new Rectangle(301, 198, 177, 30));
		contentPanel.add(idField);
		
		passwordField=new JPasswordField();
		passwordField.setFont(new Font("微软雅黑",Font.PLAIN,20));
		passwordField.setBounds(new Rectangle(301, 253, 177, 30));
		contentPanel.add(passwordField);
		

		
		
		
		
		
		title=new Title();
		title.setSize(1157, 143);
		title.setLocation(-19, -12);
		contentPanel.add(title);
		
		JButton button = new JButton("取消");
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int message=JOptionPane.showConfirmDialog(null,"取消登录？","确认",JOptionPane.YES_NO_OPTION);
				if(message==JOptionPane.YES_OPTION){
					setVisible(false);
				}
				
			}
		});
		button.setFont(new Font("微软雅黑", Font.BOLD, 20));
		button.setBounds(376, 344, 90, 30);
		contentPanel.add(button);
		
		image=new ImageIcon("Graphics/南京大学.png");
		Image temp=image.getImage();
		temp=temp.getScaledInstance(200, 300, Image.SCALE_FAST);
		image=new ImageIcon(temp);
		
//		bottomIcon=new ImageIcon("Graphics/loginGraphics/buttonBottom.png");
//		bottomLabel=new JLabel(bottomIcon);
//		bottomLabel.setBounds(0, 0, 600, 600);

//		contentPanel.add(bottomLabel,-1);
		
		
		
	}
	
	public void sendRequest(){
		ResultMessage message=userController.login(idField.getText(), passwordField.getPassword());
//		JOptionPane.showMessageDialog(null,"登录成功，正在跳转！","提示",JOptionPane.INFORMATION_MESSAGE);

//		newFrame.setVisible(true);
		if(message==(ResultMessage.LOGIN_SUCCESS)){
			userID=idField.getText();
			
			

			try {
				
				userType=userController.getUserType(userID);
				switch(userType)
				{
				case 0:
				{
					newFrame=new StudentFrame(userID);
					
					newFrame.setVisible(false);
					setVisible(false);
					break;
				}
				case 1:
				{
					newFrame=new TeacherFrame(userID);
					newFrame.setVisible(false);
					setVisible(false);
					break;
				}
				case 2:
				{
					newFrame=new DepartmentDeanFrame(userID);
					newFrame.setVisible(false);
					setVisible(false);
					break;
				}
				case 3:
				{
					newFrame=new UniversityDeanFrame(userID);
					newFrame.setVisible(false);
					setVisible(false);
					break;
				}
				default:
				{
					newFrame=new AdministratorFrame(userID);
					newFrame.setVisible(false);
					setVisible(false);
					break;
				}	
				}
				newFrame.setVisible(true);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				ExceptionHandler.networkException();
			}
			
			
			
			
			}
		

		else if(message==(ResultMessage.INVALID_ID)){
			JOptionPane.showMessageDialog(null,"非法用户ID！","提示",JOptionPane.INFORMATION_MESSAGE);
			idField.setText("");
			passwordField.setText("");
		}
		else if(message==(ResultMessage.INVALID_PASSWORD)){
			JOptionPane.showMessageDialog(null,"密码应在6-12个字符之间！","提示",JOptionPane.INFORMATION_MESSAGE);

			idField.setText("");
			passwordField.setText("");
		}
		else{
			JOptionPane.showMessageDialog(null,"用户名或密码错误，请重新登录！","提示",JOptionPane.INFORMATION_MESSAGE);
//			idField.setText("");
			passwordField.setText("");

		}
	}
	
	
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
//		
		UIManager.put("RootPane.setupButtonVisible", false);
		BeautyEyeLNFHelper.translucencyAtFrameInactive=false;
		try{
			
			org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		EventQueue.invokeLater(new Runnable(){
			public void run(){
				try{
					LoginFrame frame=new LoginFrame();
					
					frame.setVisible(true);
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		});
		
	}
}
